//
//  TalksTableViewCell.m
//  Liuhe
//
//  Created by zr on 2018/8/13.
//  Copyright © 2018年 zr. All rights reserved.
//

#import "TalksTableViewCell.h"

@interface TalksTableViewCell ()

@property (nonatomic, weak)UILabel * titleLabel;
@property (nonatomic, weak)UILabel * timeLabel;
@property (nonatomic, weak)UIImageView * contentImageView;
@property (nonatomic, weak)UIImageView * iconOneImageView;
@property (nonatomic, weak)UIImageView * iconTwoImageView;
@property (nonatomic, weak)UIImageView * iconThreeImageView;
@property (nonatomic, weak)UIButton * joinButton;
@property (nonatomic, weak)UIView * lineView;

@end

@implementation TalksTableViewCell


- (void)setModel:(TopicModel *)model{
    
    _model = model;
    
    self.titleLabel.text = model.title;
    // timeStampString 是服务器返回的13位时间戳
    NSString *timeStampString  = model.ctime;
    
    // iOS 生成的时间戳是10位
    NSTimeInterval interval    =[timeStampString doubleValue];
    NSDate *date               = [NSDate dateWithTimeIntervalSince1970:interval];
    
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat:@"yyyy-MM-dd"];
    NSString *dateString       = [formatter stringFromDate: date];
    
    self.timeLabel.text = dateString;
    
    NSURL * url = [NSURL URLWithString:model.images.firstObject];
    [self.contentImageView sd_setImageWithURL:url];
    
    NSArray * arr = model.praise;
    
    if (arr.count == 1) {
        
        self.iconOneImageView.hidden = NO;
        self.iconTwoImageView.hidden = YES;
        self.iconThreeImageView.hidden = YES;
        
        
        NSDictionary * oneDict = arr.firstObject;
        NSURL * oneUrl = [NSURL URLWithString:oneDict[@"headimg"]];
        [self.iconOneImageView sd_setImageWithURL:oneUrl];
    }else if (arr.count == 2){
        
        self.iconOneImageView.hidden = NO;
        self.iconTwoImageView.hidden = NO;
        self.iconThreeImageView.hidden = YES;
        
        NSDictionary * oneDict = arr.firstObject;
        NSURL * oneUrl = [NSURL URLWithString:oneDict[@"headimg"]];
        [self.iconOneImageView sd_setImageWithURL:oneUrl];
        
        NSDictionary * twoDict = arr[1];
        NSURL * twoUrl = [NSURL URLWithString:twoDict[@"headimg"]];
        [self.iconTwoImageView sd_setImageWithURL:twoUrl];
    }else if (arr.count == 3){
        
        self.iconOneImageView.hidden = NO;
        self.iconTwoImageView.hidden = NO;
        self.iconThreeImageView.hidden = NO;
        
        NSDictionary * oneDict = arr.firstObject;
        NSURL * oneUrl = [NSURL URLWithString:oneDict[@"headimg"]];
        [self.iconOneImageView sd_setImageWithURL:oneUrl];
        
        NSDictionary * twoDict = arr[1];
        NSURL * twoUrl = [NSURL URLWithString:twoDict[@"headimg"]];
        [self.iconTwoImageView sd_setImageWithURL:twoUrl];

        NSDictionary * threeDict = arr.lastObject;
        NSURL * threeUrl = [NSURL URLWithString:threeDict[@"headimg"]];
        [self.iconThreeImageView sd_setImageWithURL:threeUrl];
    }else if (arr.count == 0){
        
        self.iconOneImageView.hidden = YES;
        self.iconTwoImageView.hidden = YES;
        self.iconThreeImageView.hidden = YES;
    }
}


- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    
    if ([super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        
        [self setupUI];
    }
    
    return self;
}


- (void)setupUI{
    
    self.selectionStyle = UITableViewCellSelectionStyleNone;
    self.backgroundColor = WHITE_COLOR;
    
    UILabel * titleLabel = [UILabel new];
    
    titleLabel.textColor = BLACK_COLOR;
    titleLabel.font = [UIFont systemFontOfSize:15 * SCREENH6];
    titleLabel.text = @"----";
    
    self.titleLabel = titleLabel;
    
    [self.contentView addSubview:self.titleLabel];
    
    
    UILabel * timeLabel = [UILabel new];
    
    timeLabel.textColor = ColorWithRGB(0xB3B3B3);
    timeLabel.font = [UIFont systemFontOfSize:11 * SCREENH6];
    timeLabel.text = @"----";
    
    self.timeLabel = timeLabel;
    
    [self.contentView addSubview:self.timeLabel];
    
    
    UIImageView * contentImageView = [UIImageView new];
    
    contentImageView.backgroundColor = [UIColor whiteColor];
//    contentImageView.contentMode = UIViewContentModeScaleAspectFill;
    
    self.contentImageView = contentImageView;
    
    [self.contentView addSubview:self.contentImageView];
    
    UIButton * joinButton = [UIButton new];
    
    [joinButton setTitle:@" 参与 " forState:UIControlStateNormal];
    [joinButton setTitleColor:MAINCOLOR forState:UIControlStateNormal];
    joinButton.layer.borderColor = [MAINCOLOR CGColor];
    joinButton.layer.borderWidth = 1.0f;
    joinButton.titleLabel.font = [UIFont systemFontOfSize:15 * SCREENH6];
    
    
    self.joinButton = joinButton;
    
    [self.contentView addSubview:self.joinButton];
    
    
    UIImageView * iconOneImageView = [UIImageView new];
    
    iconOneImageView.backgroundColor = [UIColor lightGrayColor];
    iconOneImageView.layer.cornerRadius = 16;
    iconOneImageView.layer.masksToBounds = YES;
    
    self.iconOneImageView = iconOneImageView;
    
    [self.contentView addSubview:self.iconOneImageView];
    
    UIImageView * iconTwoImageView = [UIImageView new];
    
    iconTwoImageView.backgroundColor = [UIColor lightGrayColor];
    iconTwoImageView.layer.cornerRadius = 16;
    iconTwoImageView.layer.masksToBounds = YES;
    
    self.iconTwoImageView = iconTwoImageView;
    
    [self.contentView addSubview:self.iconTwoImageView];
    
    UIImageView * iconThreeImageView = [UIImageView new];
    
    iconThreeImageView.backgroundColor = [UIColor lightGrayColor];
    iconThreeImageView.layer.cornerRadius = 16;
    iconThreeImageView.layer.masksToBounds = YES;
    
    self.iconThreeImageView = iconThreeImageView;
    
    [self.contentView addSubview:self.iconThreeImageView];
    
    
    UIView * lineView = [UIView new];
    
    lineView.backgroundColor = ColorWithRGB(0xe6e6e6);
    
    self.lineView = lineView;
    
    [self.contentView addSubview:self.lineView];
}


- (void)layoutSubviews{
    
    [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.mas_equalTo(self.contentView).offset(15 * SCREENH6);
        make.top.mas_equalTo(self.contentView).offset(10 * SCREENH6);
    }];
    
    [self.timeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.mas_equalTo(self.titleLabel);
        make.top.mas_equalTo(self.titleLabel.mas_bottom).offset(5);
    }];
    
    [self.contentImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.mas_equalTo(self.timeLabel);
        make.trailing.mas_equalTo(self.contentView).offset(-15 * SCREENH6);
        make.height.mas_equalTo(130 * SCREENH6);
        make.top.mas_equalTo(self.timeLabel.mas_bottom).offset(10 * SCREENH6);
    }];
    
    [self.joinButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.trailing.mas_equalTo(self.contentImageView);
        make.top.mas_equalTo(self.contentImageView.mas_bottom).offset(8 * SCREENH6);
        make.height.mas_equalTo(25 * SCREENH6);
        make.width.mas_equalTo(52 * SCREENH6);
    }];
    
    [self.iconOneImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.mas_equalTo(self.contentImageView);
        make.top.mas_equalTo(self.contentImageView.mas_bottom).offset(5 * SCREENH6);
        make.height.width.mas_equalTo(32 * SCREENH6);
    }];
    
    [self.iconTwoImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.mas_equalTo(self.iconOneImageView.mas_trailing).offset(8 * SCREENH6);
        make.height.width.centerY.mas_equalTo(self.iconOneImageView);
    }];
    
    [self.iconThreeImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.mas_equalTo(self.iconTwoImageView.mas_trailing).offset(8 * SCREENH6);
        make.height.width.centerY.mas_equalTo(self.iconTwoImageView);
    }];
    
    [self.lineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.trailing.mas_equalTo(self.contentView);
        make.height.mas_equalTo(2 * SCREENH6);
        make.bottom.mas_equalTo(self.contentView);
    }];
}

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
